package com.hpf.lease.web.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hpf.lease.common.config.CommonException;
import com.hpf.lease.common.result.ResultCodeEnum;
import com.hpf.lease.model.bean.LabelInfo;
import com.hpf.lease.web.admin.mapper.LabelInfoMapper;
import com.hpf.lease.web.admin.service.LabelInfoService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author GOD
* @description 针对表【label_info(标签信息表)】的数据库操作Service实现
* @createDate 2024-08-29 15:00:50
*/
@Service
public class LabelInfoServiceImpl extends ServiceImpl<LabelInfoMapper, LabelInfo>
    implements LabelInfoService {

    @Override
    public boolean saveOrUpdateInfo(LabelInfo labelInfo) {
        Long id = labelInfo.getId() == null ? -1L : labelInfo.getId();
        LabelInfo one = this.getOne(Wrappers.lambdaQuery(LabelInfo.class)
                .eq(LabelInfo::getType, labelInfo.getType())
                .eq(LabelInfo::getName, labelInfo.getName())
                .last("limit 1"));
        if (one != null && one.getId().equals(id)){
            throw new CommonException(ResultCodeEnum.LABEL_NAME_EXISTS_ERROR);
        }
        return this.saveOrUpdate(labelInfo);
    }

    @Override
    public List<LabelInfo> selectAll(Long type) {
        LambdaQueryWrapper<LabelInfo> wrapper = null;
        if (type != null){
            wrapper = Wrappers.lambdaQuery(LabelInfo.class).eq(LabelInfo::getType,type);
        }
        return this.list(wrapper);
    }
}




